define(function () {
    return function (pane) {
        /**
         * 导航
         */
        {
            pane.html(utils.getHtml('default_table'));

            utils.addFilterGroupbtn(pane, {
                text: '全部',
                name: 'type',
                filters: dicts.article_type
            });

            utils.addFilterGroupbtn(pane, {
                text: '全部',
                name: 'status',
                filters: dicts.status
            });
        }

        /**
         * 统计
         */
        {
            var analyse = function () {
                adminApi('articles_analyse').then(function (rows) {
                    $('.content-header .filter-type[data-name="type"] .badge').text('');
                    var total = 0;
                    for (var i in rows) {
                        var row = rows[i];
                        $('.type-' + row.type, pane).text(row.count || 0);
                        total += (row.count || 0);
                    }
                    $('.type-all', pane).text(total);
                });
            }
            analyse();
        }

        //全局变量
        var article_fields = utils.convertFields([
            ['type', 'select', '类别', false, dicts.article_type],
            ['slug', 'text', '地址'],
            ['title', 'text', '标题', {notEmpty: {message: '标题不能为空'}}],
            ['feature', 'select', '推荐', false, dicts.boolean],
            ['description', 'textarea', '描述'],
            ['keywords', 'textarea', '关键字'],
            ['view_count', 'text', '浏览次数'],
            ['order_by', 'text', '排序'],
            ['status', 'select', '状态', false, dicts.status]
        ]);

        //初始化工具
        utils.addTools(pane, [
            {className: 'add-article-btn', icon: 'fa-plus', text: '添加文章'},
            {className: 'refresh-btn', icon: 'fa-refresh', text: '刷新'}
        ]);
        $(pane).on('click', '.add-article-btn', function () {
            utils.formAdd(table, {
                name: '文章', fields: article_fields, columns: 6, data: {order_by: 0},
                fnServerData: function (paramData) {
                    return adminApi('articles_add', paramData);
                }
            });
        }).on('click', '.refresh-btn', function () {
            if (table) {
                table.api().draw();
                analyse();
            }
        });

        //初始化表格
        var table = utils.dataTable(pane, {
            columns_arrays: [
                ["id", "#"],
                ["image", "图片", false, utils.imageRender()],
                ["slug", "地址", false],
                ["title", "标题", false],
                ["view_count", "浏览", false],
                ["feature", "推荐", false, utils.dictRender('boolean')],
                ["order_by", "排序", false],
                ["status", "状态", false, utils.dictRender('status')],
                ["created_at", "创建日期", false, utils.dateRender()]
            ],
            sorting: [[0, 'desc']],
            fnServerData: function (paramData) {
                return adminApi('articles_browse', paramData);
            }
        });

        //编辑开关
        var toggleHandler = utils.toggleChild(pane, 0, function (checked) {
            table.api().rows().every(function (idx) {
                var child = this.child, data = this.data();
                if (checked == 1) {
                    var opts = [
                        {text: '图片管理', icon: 'fa-image', className: 'image-ope'},
                        {text: '横幅管理', icon: 'fa-image', className: 'banner-ope'},
                        {text: '编辑文章', icon: 'fa-edit', className: 'edit-ope'},
                        {text: '编辑详情', icon: 'fa-file-code-o', className: 'article-ope'},
                        {text: '删除文章', icon: 'fa-trash-o', className: 'delete-ope btn-danger'}
                    ];
                    child(utils.childWrapper(idx, opts)).show();
                } else {
                    child.hide();
                }
            });
        });

        //初始化事件
        table.on('draw.dt', toggleHandler).on('click', '.image-ope', utils.tableEvent(table, function (idx, rowData) {
            //图片管理
            var tools = {
                image: {text: '默认', image: rowData.image},
                remove: {text: '删除', className: 'btn-danger'}
            };
            var setImage = function (data) {
                rowData.image = data.image;
                tools.image.image = data.image;
                table.fnUpdate(rowData, idx, undefined, false);
            }
            utils.uploader({
                title: '文章 - ' + rowData.title,
                tools: tools,
                fnImages: function () {
                    return adminApi('articles_image_browse', {
                        type: 'ArticleImage',
                        reference: rowData.id
                    });
                },
                fnHandle: function (data) {
                    data.type = 'ArticleImage';
                    data.reference = rowData.id;
                    return adminApi('articles_image_add', data).then(function (result) {
                        if (result.image) {
                            setImage(result.data);
                        }
                        return result;
                    });
                },
                fnOperate: function (data) {
                    return adminApi('articles_image_edit', data).then(function (result) {
                        if (result.success && data.operation === 'image') {
                            setImage(result.data);
                        }
                        return result;
                    });
                }
            });
        })).on('click', '.banner-ope', utils.tableEvent(table, function (idx, rowData) {
            //横幅管理
            var tools = {
                banner: {text: '默认', image: rowData.banner},
                remove: {text: '删除', className: 'btn-danger'}
            };
            var setImage = function (data) {
                rowData.banner = data.image;
                tools.banner.image = data.image;
                table.fnUpdate(rowData, idx, undefined, false);
            }
            utils.uploader({
                title: '广告 - ' + rowData.title,
                tools: tools,
                fnImages: function () {
                    return adminApi('articles_image_browse', {
                        type: 'ArticleBanner',
                        reference: rowData.id
                    });
                },
                fnHandle: function (data) {
                    data.type = 'ArticleBanner';
                    data.reference = rowData.id;
                    return adminApi('articles_image_add', data).then(function (result) {
                        if (result.banner) {
                            setImage(result.data);
                        }
                        return result;
                    });
                },
                fnOperate: function (data) {
                    return adminApi('articles_image_edit', data).then(function (result) {
                        if (result.success && data.operation === 'banner') {
                            setImage(result.data);
                        }
                        return result;
                    });
                }
            });
        })).on('click', '.edit-ope', utils.tableEvent(table, function (idx, rowData) {
            //编辑
            adminApi('articles_load', {id: rowData.id}).then(function (data) {
                utils.formEdit(table, {
                    idx: idx, name: rowData.title, fields: article_fields, columns: 6, data: data,
                    fnServerData: function (paramData) {
                        return adminApi('articles_edit', paramData);
                    }
                });
            });
        })).on('click', '.delete-ope', utils.tableEvent(table, function (idx, rowData) {
            //删除
            utils.confirmation(this, {
                placement: 'top',
                optName: '删除文章',
                title: '你确定删除文章?',
                fnConfirm: function () {
                    return adminApi('articles_delete', {id: rowData.id}).then(utils.handDelete(table, {name: '文章'}));
                }
            });
        })).on('click', '.article-ope', utils.tableEvent(table, function (idx, rowData) {
            //编辑详情
            utils.editor({
                id: rowData.id,
                title: '详情编辑 - ' + rowData.title,
                serverUrl: UEDITOR_API_URL + '?type=ArticleContent&reference=' + rowData.id,
                ready: function (ue) {
                    adminApi('articles_load', {id: rowData.id}).then(function (article) {
                        if (article.content) {
                            ue.setContent(article.content);
                        }
                    });
                },
                submit: function (html) {
                    return adminApi('articles_edit', {
                        id: rowData.id, content: html
                    });
                }
            });
        }));

        table.analyse = analyse;
    }
});